package com.lanhai.zhonglai.dispatch;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lanhai.zhonglai.MsgCore;
import com.lanhai.zhonglai.beans.AgvBean;
import com.lanhai.zhonglai.beans.AgvMountBean;
import com.lanhai.zhonglai.beans.DotBean;
import com.lanhai.zhonglai.beans.StationBean;
import com.lanhai.zhonglai.beans.StationMountBean;
import com.lanhai.zhonglai.enums.AgvCurrentTransEnum;
import com.lanhai.zhonglai.enums.AgvMountTypeEnum;
import com.lanhai.zhonglai.enums.TransDirectionEnum;
import com.lanhai.zhonglai.interfaces.IAgv;
import com.lanhai.zhonglai.interfaces.IAgvEvent;
import com.lanhai.zhonglai.interfaces.ICmdLog;
import com.lanhai.zhonglai.interfaces.IControlArea;
import com.lanhai.zhonglai.interfaces.IDot;
import com.lanhai.zhonglai.interfaces.IDotEvent;
import com.lanhai.zhonglai.interfaces.IRoad;
import com.lanhai.zhonglai.interfaces.ISendData;
import com.lanhai.zhonglai.interfaces.IStation;
import com.lanhai.zhonglai.interfaces.IStationMount;
import com.lanhai.zhonglai.interfaces.IStationProtocol;
import com.lanhai.zhonglai.third.StationFactory;

/**
 * ALD干法
 * @author zhengwei
 *
 */
@Component
public class JudgementALD_3 {
	@Autowired
	IStation iStation;

	@Autowired
	IStationMount iStationMount;

	@Autowired
	IAgv iAgv;

	@Autowired
	IDot iDot;

	@Autowired
	ISendData sendTools;

	@Autowired
	IAgvEvent iAgvEvent;

	@Autowired
	IDotEvent iDotEvent;

	@Autowired
	IRoad iRoad;

	@Autowired
	ICmdLog iCmdLog;

	@Autowired
	IControlArea iControlArea;

	@Autowired
	AgvCmdMap agvCmdMap;

	@Autowired
	StationLeaveCheck stationCheck;
	
 
	
	public void judgement (AgvBean agv, StationBean station, DotBean dot, AgvMountBean agvMount,
			StationMountBean dmount) {
		IStationProtocol stationEvent = StationFactory.getStation(station.getCmdSign());
		
		
		if (StationCmdMap.canLeave(station.getDotno())) { 
			 if(agvMount.getMountType()==AgvMountTypeEnum.AllFull) {
				 //假离开
				 StationCmdMap.clearState(dot.getDotno());
				 MsgCore.sendMsg(agv.getAgvId(), "继续等待下一次传输");
				 agv.setFakeLeave(true);
				 agv.setBothTrans(true);
				 agv.setNotClear(true);  //还有一层要传输，不要在"xx完成"处理中清空传输状态
				 iAgv.saveCache(agv);
				 
					byte[] goout = stationEvent.agvLeave(station.getLine(), station.getFillType());
					sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV假离开", goout,
							station.getStationname());
				 
				 
			 }else {
				 //真离开
				 byte[] cmd = stationEvent.agvLeave(station.getLine(), station.getFillType());
					sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV离开", cmd,
							station.getStationname());
				 stationCheck.check(dot, agv, station, stationEvent, agvMount);
			 } 
			return; 
		}
		
		//允许传输
		if (StationCmdMap.canReceive(station.getDotno())&&agv.isFinishTrans()==false && agv.getCurrentTrans()!=AgvCurrentTransEnum.None) { 
			// 传输过程中，直接过滤 
			if (agv.isTransing()==false) {
				 agv.setTransing(true);
				 iAgv.saveCache(agv);
				switch (agv.getCurrentTrans()) {
				 //只有出料的动作
				case DownIn:
					iAgvEvent.transDown(agv, true);
					break;
				case DownOut:
					iAgvEvent.transDown(agv, false);
					break;
				case UpIn:
					iAgvEvent.transUp(agv, true);
					break;
				case UpOut:
					iAgvEvent.transUp(agv, false);
					break;
				case UpInAndDownOut:
					iAgvEvent.transUpInAndDownOut(agv);
					break;
				case UpOutAndDownIn:
					iAgvEvent.transUpOutAndDownIn(agv);
					break;
				default:
					MsgCore.sendMsg(agv.getAgvId(), "未知的传输模式");
					break;
				}
			}
			return;
		} 
		
		if(agvMount.getMountType()==AgvMountTypeEnum.AllEmpty && agv.isUpHasTrans() ) {
			//只出料模式，全空
			agv.setNotClear(false); //可以跳出单层完成了
			agv.setCurrentTrans(AgvCurrentTransEnum.None);
			 agv.setFinishTrans(true); 
		}
		if(agvMount.getMountType()==AgvMountTypeEnum.UpEmptyAndDownFull  ) {
			//上满下空
			agv.setNotClear(false);//可以跳出单层完成了
			agv.setFinishTrans(true);
			agv.setCurrentTrans(AgvCurrentTransEnum.None);
		}
		iAgv.saveCache(agv);
		
		
		if(agv.isFinishTrans()) {
			//申请离开
			MsgCore.sendMsg(agv.getAgvId(), "AGV申请离开");
			
			byte[] goout = stationEvent.applyLeave(station.getLine(), station.getFillType()); 
			sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV申请离开", goout,
					station.getStationname());
			return;
		}
		
		if (StationCmdMap.canTrans(station.getDotno())  ) {
			if(dmount.isAllowIn() && agvMount.getMountType()==AgvMountTypeEnum.AllFull) {
				//只有上层传出 
				send(agv, station, stationEvent);
				return;
			}
			if(dmount.isAllowIn()&&dmount.isAllowOut() && agvMount.getMountType()==AgvMountTypeEnum.UpFullAndDownEmpty) {
				//AGV上出下入 
				both(agv, station, stationEvent);
				return;
			}
			if(dmount.isAllowOut() && agvMount.isDownFull()==false) {
				//下层收货 
				receive(agv, station, stationEvent);
				return;
			} 
			if(dmount.isAllowIn() && agvMount.isUpFull()) {
				//只有上层传出
				send(agv, station, stationEvent);
				return;
			}
			MsgCore.sendMsg(agv.getAgvId(), String.format("待传输花篮数量，上料%d,出料%d", dmount.getInNum(),dmount.getOutNum()) );
			return; 
		}
		
		
		if(agvMount.getMountType()==AgvMountTypeEnum.AllFull) {
			if(agv.isFakeLeave()==false) {
				byte[] goout = stationEvent.applyLeave(station.getLine(), station.getFillType()); 
				sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV申请离开", goout,
						station.getStationname());
			}else {
				//全满
				if(dmount.isAllowIn()) {
					//AGV就位
					agvArrived(station,stationEvent);
				} else {
					MsgCore.sendMsg(agv.getAgvId(), "料不够，不发起到位"); 
				}
			}
			
		}else if(agvMount.getMountType()==AgvMountTypeEnum.AllEmpty ) {
			//全空
			if(dmount.isAllowOut()) {
				//AGV就位
				agvArrived(station,stationEvent);
			} else {
				MsgCore.sendMsg(agv.getAgvId(), "料不够，不发起到位");
			
			} 
		}else if(agvMount.getMountType()==AgvMountTypeEnum.UpFullAndDownEmpty) {
			//上空下满
			if(dmount.getOutNum()==9) {
				MsgCore.sendMsg(agv.getAgvId(), "等满10个");
				return;  //9个的时候等 
			}
			if(dmount.isAllowOut()||dmount.isAllowIn()) {
				//AGV就位
				agvArrived(station,stationEvent);
			} else {
				MsgCore.sendMsg(agv.getAgvId(), "料不够，不发起到位");
			
			} 
		}else {
			MsgCore.sendMsg(agv.getAgvId(),  "机台前的小车错误的状态" );
			
		}
		
	}
	
	void agvArrived(StationBean station,IStationProtocol stationEvent) {
		byte[] cmd = stationEvent.agvArrived(station.getLine(), TransDirectionEnum.Both);
		sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV就位", cmd,
				station.getStationname());
	}
	
	void receive(AgvBean agv,StationBean station,IStationProtocol stationEvent) {
		agv.setCurrentTrans(AgvCurrentTransEnum.DownIn);  
		agv.setApplyTransType(TransDirectionEnum.OnlyOut);
		iAgv.saveCache(agv);
		MsgCore.sendMsg(agv.getAgvId(), "AGV发起申请传输-AGV传入");
		byte[] cmd = stationEvent.applyRecieve(station.getLine(), TransDirectionEnum.OnlyOut);
		sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV申请传输", cmd,
				station.getStationname());  
	}
	
	void send(AgvBean agv,StationBean station,IStationProtocol stationEvent) {
		agv.setCurrentTrans(AgvCurrentTransEnum.UpOut);  
		agv.setApplyTransType(TransDirectionEnum.OnlyIn);
		iAgv.saveCache(agv);
		MsgCore.sendMsg(agv.getAgvId(), "AGV发起申请传输-AGV传出");
		byte[] cmd = stationEvent.applyRecieve(station.getLine(), TransDirectionEnum.OnlyIn);
		sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV申请传输", cmd,
				station.getStationname());  
	}
	
	void both(AgvBean agv,StationBean station,IStationProtocol stationEvent) {
		 agv.setCurrentTrans(AgvCurrentTransEnum.UpOutAndDownIn); 
		agv.setApplyTransType(TransDirectionEnum.Both);
		agv.setBothTrans(true);
		iAgv.saveCache(agv);

		byte[] cmd = stationEvent.applyRecieve(station.getLine(), TransDirectionEnum.Both);
		sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV申请双层", cmd,
				station.getStationname()); 
	}
	
}